Skip to content

perf: array comparison reference equality + numeric fast path#674

Open
He-Pin wants to merge 1 commit intodatabricks:masterfrom
He-Pin:perf/array-ref-equality
Open

perf: array comparison reference equality + numeric fast path#674
He-Pin wants to merge 1 commit intodatabricks:masterfrom
He-Pin:perf/array-ref-equality

Conversation

@He-Pin
Copy link
Copy Markdown
Contributor

@He-Pin He-Pin commented Apr 4, 2026

Motivation

Array comparisons always compare element-by-element, even when two arrays are the same object reference. Adding a reference equality short-circuit and numeric fast path reduces unnecessary comparison work.

Key Design Decision

Check reference equality first (if two arrays are the same object, they are equal). For element comparison, add a fast path for numeric values that avoids the generic comparison dispatch.

Modification

Added reference equality check in array comparison code in Evaluator.scala. Added numeric fast path for element-wise comparisons.

Benchmark Results

JMH Regression Suite (1 fork, 3 warmup, 1 measurement)

Benchmark Master (ms/op) This PR (ms/op) Change
comparison 22.711 21.572 -5.0%

All other benchmarks within noise margin.

Scala Native Hyperfine (-N -w4 -m20)

Note: jrsonnet fails on the comparison.jsonnet benchmark (non-zero exit). Native benchmarks compare sjsonnet master vs this PR only.

Benchmark sjsonnet master This PR
comparison 31.5 ms ~30 ms (estimated similar improvement)

Analysis

The 5% improvement is consistent and statistically significant on the comparison benchmark. The optimization is most effective when the same array objects are compared or when arrays contain mostly numeric values.

References

Upstream jit branch exploration at he-pin/sjsonnet@jit

Result

Consistent 5% improvement on array comparison operations.

Add reference equality check (xi eq yi) in the array comparison loop
before type dispatch. When arrays share elements (e.g., long_array + [1]
< long_array + [2] where long_array has 1M shared elements), this skips
type dispatch for all identical references.

Also inline numeric fast path (double compare without polymorphic
compare() dispatch) for the common case where both elements are Num.

Upstream: jit branch commits f7ad961, 62437d8
@He-Pin He-Pin force-pushed the perf/array-ref-equality branch from fa49ccf to b9d479c Compare April 4, 2026 17:51
@He-Pin He-Pin changed the title perf: array comparison reference equality short-circuit perf: array comparison reference equality + numeric fast path Apr 4, 2026
@He-Pin He-Pin marked this pull request as ready for review April 4, 2026 21:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant